//发送
function send(){
    sendFromWaitSend(grid);
}

function rend(txt, data, r, c,col) {
    if(col.name === "subject"){
    	//标题列加深
	    txt="<span class='grid_black titleText'>"+txt+"</span>";
	    //加图标
	    //重要程度
	    if(data.importantLevel !=""&& data.importantLevel != 1){
	        txt = "<span style='float: left;' class='ico16 important"+data.importantLevel+"_16 '></span>"+ txt ;
	    }
	    //附件
	    if(data.hasAttsFlag === true){
	        txt = txt + "<span class='ico16 affix_16'></span>" ;
	    }
	    //协同类型
	    if(data.bodyType!==""&&data.bodyType!==null&&data.bodyType!=="10"&&data.bodyType!=="30"){
	        txt = txt+ "<span class='ico16 office"+data.bodyType+"_16'></span>";
	    }
	    txt = "<a class='color_black' href='javascript:void(0)'>"+txt+"</a>";
        return txt;
    } else if (col.name=='subState') {
        var key = "collaboration.substate.new."+data.subState+".label";
        txt = $.i18n(key);
        return txt;
    } else {
    	return txt;
    }
}
   
function transmitCol(){
	transmitColFromGrid(grid,"listWaitSend");
}

 //删除
function deleteCol(){
	  var rows = grid.grid.getSelectRows();
	  
	  if(rows.length <= 0) {
          // 请选择要删除的协同。
      $.alert($.i18n('collaboration.grid.selectDelete'));
      	return true;
	  }
	  var objSummarys = [];
	  var objAffairs =[];
	  var objRow;
	  for (var i = 0; i < rows.length; i++) {
		  objRow = rows[i];
		  objSummarys.push(objRow.summaryId);
		  objAffairs.push(objRow.affairId);
	  }
	  var idMap ={
			  "summaryID":objSummarys,
			  "affairID":objAffairs
	  }
	//js事件接口
	var sendDevelop = $.ctp.trigger('beforeWaitSendDelete',idMap);
	if(!sendDevelop){
	   // $.alert($.i18n('collaboration.page.js.third.error.alert.js'));
		 return;
	}
	
    deleteItems('draft',grid,'listWaitSend',paramMethod);
}
function edit(){
    editFromWaitSend(grid);
}


//定义setTimeout执行方法
var TimeFn = null;
function clickRow(data,rowIndex, colIndex) {
    // 取消上次延时未执行的方法
    clearTimeout(TimeFn);
    TimeFn = setTimeout(function(){
    	grid.grid.resizeGridUpDown('down');
        var url = _ctxPath + "/collaboration/collaboration.do?method=summary&openFrom=listWaitSend&affairId="+data.affairId;
        var title = data.subject;
        doubleClick(url,escapeStringToHTML(title));
    },300);
}

function dbclickRow(){
    // 取消上次延时未执行的方法
    clearTimeout(TimeFn);
    grid.grid.resizeGridUpDown('down');
    editFromWaitSend(grid);
}

 var grid ;
 var searchobj;
 var layoutObj=null;
$(document).ready(function () {
    layoutObj=new MxtLayout({
        'id': 'layout',
        'northArea': {
            'id': 'north',
            'height': 40,
            'sprit': false,
            'border': false
        },
        'centerArea': {
            'id': 'center',
            'border': false,
            'minHeight': 20
        }
    });
    
    var nodePolicy = $.parseJSON(pTemp.nodePolicy);
    var submenu = new Array();
    //判断是否有新建协同的资源权限，如果没有则屏蔽转发协同
    if ($.ctx.resources.contains('F01_newColl')) {
    	if(nodePolicy.forward){
	    	submenu.push({name:  $.i18n('collaboration.transmit.col.label'),click:transmitCol});
    	}
    };
    //判断是否有转发邮件的资源权限，如果没有则屏蔽转发协同
    // shkk 2019-08-28 取消电子审批事项中转发邮件按钮的显示 start 
    /*if ($.ctx.resources.contains('F12_mailcreate')) {
        //邮件
    	if (emailShow) {
    		if(nodePolicy.forward){
    			submenu.push({name:  $.i18n('collaboration.transmit.mail.label'),click:transmitMail});
    		}
    	}
    };*/
    // shkk end
    //取新建节点权限
    var nodePolicy = $.parseJSON(pTemp.nodePolicy);
    var toolbarArray = new Array();
    //发送
    toolbarArray.push({id: "send",name: $.i18n('collaboration.newcoll.send'),className: "ico16 send_16",click:send});
    //编辑
    toolbarArray.push({id: "edit",name:$.i18n('collaboration.edit.label'),className: "ico16 editor_16",click:edit});
    //转发
    if(submenu.length >0 ){
    	toolbarArray.push({id: "transmit",name: $.i18n('collaboration.transmit.label'),className: "ico16 forwarding_16",subMenu: submenu});
    }
    //删除
    if(nodePolicy.reMove){
    	toolbarArray.push({id: "delete",name: $.i18n('collaboration.button.delete.label'),className: "ico16 del_16",click:deleteCol});
    }
    //toolbar扩展
    for (var i = 0;i<addinMenus.length;i++) {
        toolbarArray.push(addinMenus[i]);
    }
    $("#toolbars").toolbar({
        toolbar: toolbarArray
    });
    //搜索框
    var topSearchSize = 7;
    if($.browser.msie && $.browser.version=='6.0'){
        topSearchSize = 10;
    }
    
    //查询条件
    var condition = new Array();
    condition.push({
        id: 'title',
        name: 'title',
        type: 'input',
        text: $.i18n("common.subject.label"),//标题
        value: 'subject',
        maxLength:100
    });
    condition.push({
        id: 'importent',
        name: 'importent',
        type: 'select',
        text: $.i18n("common.importance.label"),//重要程度
        value: 'importantLevel',
        items: [{
            text: $.i18n("common.importance.putong"),//普通
            value: '1'
        }, {
            text: $.i18n("common.importance.zhongyao"),//重要
            value: '2'
        }, {
            text: $.i18n("common.importance.feichangzhongyao"),//非常重要
            value: '3'
        }]
    });
    condition.push({
        id: 'datetime',
        name: 'datetime',
        type: 'datemulti',
        text: $.i18n("common.date.create.label"),//创建时间
        value: 'createDate',
        ifFormat:'%Y-%m-%d',
        dateTime: false
    });
    condition.push({
        id: "subStateName",
        name: 'subStateName',
        type: 'select',
        text: $.i18n('common.coll.state.label'),//状态
        value: 'subState',
        items: [{
            text: $.i18n("collaboration.substate.3.label"),//撤销
            value: '3'
        }, {
            text:$.i18n("collaboration.substate.1.label"),//草稿
            value: '1'
        }, {
          text: $.i18n("collaboration.substate.new.2.label"),//回退
          value: '2'
        }]
    });
    searchobj = $.searchCondition({
        top:topSearchSize,
        right:85,
        searchHandler: function(){
            var val = searchobj.g.getReturnValue();
            
            if(val !== null){
                $("#listWaitSend").ajaxgridLoad(getSearchValueObj());
                var _summarySrc =  $('#summary').attr("src");
                if(_summarySrc.indexOf("listDesc") != -1){
                	setTimeout(function(){
                		$('#summary').attr("src","listDesc.do?method=listDesc&type=listWaitSend&size="+grid.p.total+"&r=" + Math.random() + CsrfGuard.getUrlSurffix());	
                	},1000);
                }
            }
        },
        conditions:condition
    });
    //表格加载
        grid = $('#listWaitSend').ajaxgrid({
        colModel: [{
            display: 'id',
            name: 'affairId',
            width: 'smallest',
            type: 'checkbox',
            align:'center'
        }, {
            display: $.i18n("common.subject.label"),//标题
            name: 'subject',
            sortable : true,
            width: 'big'
        }, {
          display: $.i18n("common.coll.state.label"),//状态
          name: 'subState',
          sortable : true,
          width: 'small'
        }, {
            display: $.i18n("common.date.create.label"),//创建时间
            name: 'createDate',
            sortable : true,
            width: 'medium'
        }, {
            display: $.i18n("collaboration.process.cycle.label"),//流程期限
            name: 'processDeadLineName',
            sortable : true,
            width: 'medium'
        }],
        click: clickRow,
        dblclick: dbclickRow,
        render:rend,
        height: 200,
        noTotal:isShowTotal=="0" ? true : false,
        gridType:'autoGrid',
        showTableToggleBtn: true,
        isHaveIframe:true,
        managerName : "colManager",
        managerMethod : "getWaitSendList",
        parentId: $('.layout_center').eq(0).attr('id'),
        slideToggleBtn:true,
        vChange: true,
        vChangeParam: {
            overflow: "hidden",
            autoResize:false //表格下方是否自动显示
        }
    });
    
    
    //页面底部说明加载
    $('#summary').attr("src","listDesc.do?method=listDesc&type=listWaitSend&size="+grid.p.total+"&r=" + Math.random() + CsrfGuard.getUrlSurffix());
    
  
});   

//二维码传参chenxd
function precodeCallback(){
	var obj = getSearchValueObj();
	obj.openFrom = "listWaitSend";
	return obj;
}

function getSearchValueObj(){
	 var o = new Object();
     var templeteIds = $.trim(_paramTemplateIds);
     if(templeteIds != ""){
         o.templeteIds = templeteIds;
     }
     var choose = $('#'+searchobj.p.id).find("option:selected").val();
     if(choose === 'subject'){
         o.subject = $('#title').val();
     }else if(choose === 'importantLevel'){
         o.importantLevel = $('#importent').val();
     }else if(choose === 'createDate'){
         var fromDate = $('#from_datetime').val();
         var toDate = $('#to_datetime').val();
         if(fromDate != "" && toDate != "" && fromDate > toDate){
             $.alert($.i18n('collaboration.rule.date'));//开始时间不能大于结束时间
             return;
         }
         var date = fromDate+'#'+toDate;
         o.createDate = date;
     }else if(choose === 'subState'){
         o.subState = $('#subStateName').val();
     }
     if(window.location.href.indexOf("condition=templeteAll&textfield=all") != -1){
     	o.templeteAll="all";
  	}
     return o;
}

function showAdvanceSearch(){
	openQueryViews('listWaitSend', !advanceSearchFlag);
}